What is @egjs/list-differ?
@egjs/list-differ is a lightweight JavaScript library designed to efficiently compute the differences between two lists. It is particularly useful for applications that need to update the DOM or other data structures based on changes in a list, such as in virtual DOM implementations or state management libraries.
What are @egjs/list-differ's main functionalities?
Compute Differences Between Lists
This feature allows you to compute the differences between two lists. The `update` method returns an object that describes the changes needed to transform the old list into the new list.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['b', 'c', 'd'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result);
Track Insertions and Deletions
This feature allows you to track which items have been added or removed from the list. The `added` and `removed` properties of the result object provide the indices of the added and removed items, respectively.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['b', 'c', 'd'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result.added); // [2]
console.log(result.removed); // [0]
Track Moved Items
This feature allows you to track items that have been moved within the list. The `maintained` property of the result object provides the indices of the items that have been moved.
const { ListDiffer } = require('@egjs/list-differ');
const oldList = ['a', 'b', 'c'];
const newList = ['c', 'a', 'b'];
const differ = new ListDiffer();
const result = differ.update(oldList, newList);
console.log(result.maintained); // [2, 0, 1]
Other packages similar to @egjs/list-differ
diff
The 'diff' package provides a way to compute the differences between two sequences, including lists and strings. It offers a more general-purpose diffing algorithm compared to @egjs/list-differ, which is specifically optimized for list diffing.
fast-diff
The 'fast-diff' package is a lightweight library for computing the differences between two strings. While it is optimized for string diffing, it can also be adapted for list diffing, though it may not be as efficient as @egjs/list-differ for that specific use case.
deep-diff
The 'deep-diff' package allows you to compute the differences between two JavaScript objects, including nested structures. It is more versatile than @egjs/list-differ but may be overkill if you only need to diff flat lists.
list-differ
![Coverage Status](https://coveralls.io/repos/github/naver/egjs-list-differ/badge.svg?branch=master)
A module that checks the diff when values are added, removed, or changed in an array.
Installation
$ npm i @egjs/list-differ
Related Projects
How to use
checks the diff in array
import ListDiffer from "@egjs/list-differ";
const differ = new ListDiffer([1, 2, 3, 4, 5, 6, 7], e => e);
const result = differ.update([4, 3, 6, 2, 1, 7]);
console.log(result.prevList);
console.log(result.list);
console.log(result.added);
console.log(result.removed);
console.log(result.changed);
console.log(result.changedBeforeAdded);
console.log(result.changedAfterAdded);
console.log(result.orderedBeforeAdded);
console.log(result.orderedAfterAdded);
console.log(result.maintained);
What is changed?
Tuple arrays of index of prevList
and list
with different indexes from prevList
and list
| changed: |
---|
| [[3, 0], [2, 1], [1, 3], [0, 4], [6, 5]] |
prevList | ![prev_list](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_prev_list.png) |
list | ![list](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_list.png) |
What is beforeAdded, afteradded?
- beforeAdded: Obtain a minimum of
changed
in time before data is added
- changedBeforeAdded: Tuple arrays of index of
prevList
and list
with different indexes from prevList
and list
before data is added - orderedBeforeAdded: Tuple arrays of
prevIndex
and nextIndex
to change the order of prevList
to list
before data is added.
- afterAdded: Obtain a minimum of
changed
in time after data is added
- changedAfterAdded: Tuple arrays of index of
prevList
and list
with different indexes from prevList
and list
after data is added - orderedAfterAdded: Tuple arrays of
prevIndex
and nextIndex
to change the order of prevList
to list
after data is added.
| beforeAdded | | afterAdded |
---|
| removed -> ordered -> added | | removed -> added -> ordered |
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) | prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) |
removed [5, 4] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/removed.png) | removed [5, 4] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/removed.png) |
| | added [2] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_after_added.png) |
| orderedBeforeAdded: | | orderedAfterAdded: |
| [[3, 0], [3, 1], [3, 2]] | | [[4, 0], [4, 1], [3, 4], [2, 4]] |
ordered0 [3 => 0] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_before_ordered0.png) | ordered0 [4 => 0] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_after_ordered0.png) |
ordered1 [3 => 1] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_before_ordered1.png) | ordered1 [4 => 1] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_after_ordered1.png) |
ordered2 [3 => 2] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_before_ordered2.png) | ordered2 [3 => 4] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_after_ordered2.png) |
| | ordered3 [2 => 4] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_after_ordered3.png) |
added [2] | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/ordered_before_added.png) | | |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) | list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) |
| changedBeforeAdded: | | chanedAfterAdded: |
| [[3, 0], [2, 1], [1, 3]] | | [[3, 0], [2, 1], [1, 3], [0, 4]] |
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_before_prev_list.png) | prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_after_prev_list.png) |
process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_before_animation.gif) | process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_after_animation.gif) |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_before_list.png) | list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_after_list.png) |
Examples for (prevList => list)
import ListDiffer, { diff } from "@egjs/list-differ";
const prevList = [1, 2, 3, 4, 5, 6, 7];
const list = [4, 3, 6, 2, 1, 7];
const result = diff(prevList, list, e => e);
prevList => (removed => orderedBeforeAdded => added) => list
| removed => orderedBeforeAdded => added |
---|
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) |
process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_before_animation.gif) |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) |
const nextList = prevList.slice();
result.removed.forEach(index => {
nextList.splice(index, 1);
});
result.orderedBeforeAdded.forEach(([from, to], i) => {
nextList.splice(from, 1);
nextList.splice(to, 0, list[result.changedBeforeAdded[i][1]]);
});
result.added.forEach(index => {
nextList.splice(index, 0, list[index]);
});
console.log(nextList);
prevList => (removed => added => orderdAfterAdded) => list
| removed => added => orderdAfterAdded |
---|
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) |
process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/changed_after_animation.gif) |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) |
const nextList = prevList.slice();
result.removed.forEach(index => {
nextList.splice(index, 1);
});
result.added.forEach(index => {
nextList.splice(index, 0, list[index]);
});
result.orderedAfterAdded.forEach(([from, to], i) => {
nextList.splice(from, 1);
nextList.splice(to, 0, list[result.changedAfterAdded[i][1]]);
});
console.log(nextList);
prevList => (maintained => added) => list
| maintained => added |
---|
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) |
process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/maintained_added.gif) |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) |
const nextList = [];
result.maintained.forEach(([from, to]) => {
nextList.push(list[to]);
});
result.added.forEach(index => {
nextList.splice(index, 0, list[index]);
});
console.log(nextList);
prevList => (added => maintaind) => list
| added => maintaind |
---|
prevList | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/prev_list.png) |
process | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/added_maintained.gif) |
list | ![](https://github.com/naver/egjs-list-differ/raw/HEAD/./images/list.png) |
const nextList = [];
result.added.forEach(index => {
nextList.push(list[index]);
});
result.maintained.forEach(([from, to]) => {
nextList.splice(to, 0, list[to]);
});
console.log(nextList);
Bug Report
If you find a bug, please report to us opening a new Issues on GitHub.
License
egjs-list-differ is released under the MIT license.
Copyright (c) 2019-present NAVER Corp.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.